package com.xmx.ndmallwxapi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.xmx.common.annotation.LoginUser;
import com.xmx.common.holder.LoginUserHolder;
import com.xmx.common.utils.R;
import com.xmx.ndmallwxapi.feign.UserFeignClient;
import com.xmx.ndmallwxapi.vo.user.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("wx/address")
@Slf4j
public class AddressController {

    @Autowired
    UserFeignClient userFeignClient;

    @Resource
    LoginUserHolder loginUserHolder;

    @GetMapping("/list")
    public R list(){
        Integer userID = loginUserHolder.getCurrentUser().getId();
        if(ObjectUtil.isNull(userID))
            return R.unlogin();
        return userFeignClient.findListByUid(userID);
    }

    @GetMapping("/list2")
    public R list2(@LoginUser Integer userID){
        if(ObjectUtil.isNull(userID))
            return R.unlogin();
        log.error("userID ==================>>>{}",userID);
        Integer id = loginUserHolder.getCurrentUser().getId();
        return userFeignClient.findListByUid(id);
    }

    @GetMapping("/detail")
    public R detail( @RequestParam("id") Integer id){
        return userFeignClient.infoaddress(id);
    }

    @PostMapping("/save")
    public R save(@RequestBody AddressVo address){
        Integer id = loginUserHolder.getCurrentUser().getId();
        address.setUserId(id);
        return userFeignClient.saveaddress(address);
    }

    @DeleteMapping("/delete")
    public R delete(@RequestParam("addressId") Integer addressId){
        return userFeignClient.deleteaddress(new Integer[]{addressId});
    }



}
